package com.open.cloud.rbac.controller.permission;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.object.permission.CorePermissionAllocMenuDto;
import com.open.cloud.rbac.object.permission.CorePermissionDto;
import com.open.cloud.rbac.object.permission.CorePermissionSelectDto;
import com.open.cloud.rbac.object.permission.CorePermissionVo;
import com.open.cloud.rbac.service.permission.ICorePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-权限业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/permission")
public class CorePermissionController {

    @Resource
    private ICorePermissionService permissionService;

    @ApiOperation(value = "创建")
    @PostMapping("/add")
    public ResponseWrapper addPermissionInfo(@RequestBody CorePermissionDto obj) {
        JSONObject data = permissionService.addPermissionInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updatePermissionInfo(@RequestBody CorePermissionDto obj) {
        JSONObject data = permissionService.updatePermissionInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deletePermissionById(@RequestParam(name = "id") String id) {
        JSONObject data = permissionService.deletePermissionInfo(id);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public ResponseWrapper<IPage<CorePermissionVo>> getPermissionPage(@RequestBody CorePermissionSelectDto selectDto) {
        IPage<CorePermissionVo> data = permissionService.getPermissionPage(selectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "按PermissionID查询分配的菜单Ids")
    @GetMapping("/alloc/menus")
    public ResponseWrapper getPermissionById(@RequestParam(name = "id") String id) {
        List<String> data = permissionService.getPermissionRelMenu(id);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "分配菜单")
    @PostMapping("/rel/menu")
    public ResponseWrapper setPermissionRelMenu(@RequestBody CorePermissionAllocMenuDto allocMenuDto) {
        JSONObject data = permissionService.setPermissionAllocMenus(allocMenuDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

}

